package com.pro.framework.core;

import com.pro.framework.api.enums.IEnumStr;
import com.pro.framework.api.util.CollUtils;

import java.util.*;
import java.util.stream.Collectors;

public class EnumCache {
    private static final Map<Class<?>, Map<?, ?>> CACHE = new HashMap<>();

    @SuppressWarnings("unchecked")
    public static <E extends Enum<E> & IEnumStr> Map<String, E> getCodeMap(Class<E> enumClass) {
        return (Map<String, E>) CACHE.computeIfAbsent(enumClass, cls -> Arrays.stream(cls.getEnumConstants()).collect(Collectors.toUnmodifiableMap(e -> ((IEnumStr)e).getCode(), e -> e)));
//        cls -> CollUtils.listToMap((Collection<E>) Arrays.asList(cls.getEnumConstants()), E::getCode));

    }
}
